; 003.c


global _main

section .data 
LC0:
	db "please input a number string:",0Ah,0
	
LC1:
	db "please input n(2 or 8 or 16):",0Ah,0
	
LC2:
	db "%d",0
	
LC3:
	db "data error!!",0
	
LC4:
	db "the decimal is %ld",0Ah,0

extern _memset,_gets,_strupr,_printf,_strlen,_scanf,_exit
section .text 
_main:
	push ebp
	mov ebp,esp
	sub esp,0x1000
	
	mov dword [ebp-0x1000],0 ; t1
	mov dword [ebp-0xffc],0 ; i
	mov dword [ebp-0xff8],0 ; n
	mov dword [ebp-0xff4],0 ; t 
	mov dword [ebp-0xff0],0 ; t3 
	mov dword [ebp-0xfe0],0 ; a 
	
	mov dword [esp+8],100
	mov dword [esp+4],0
	lea eax,[ebp-0xfe0] ; a
	mov [esp+0],eax 
	call _memset 
	sub esp,12
	
	mov dword [esp+0],LC0
	mov eax,0
	call _printf 
	sub esp,4
	
	lea eax,[ebp-0xfe0] ;a
	mov [esp+0],eax 
	call _gets
	sub esp,4
	
	lea eax,[ebp-0xfe0] ;a
	mov [esp+0],eax 
	call _strupr
	sub esp,4

	lea eax,[ebp-0xfe0] ;a
	mov [esp+0],eax 
	call _strlen 
	sub esp,4
	mov [ebp-0xff0],eax ; t3 
	
	mov dword [ebp-0x1000],0 ; t1 
	
	mov dword [esp+0],LC1
	mov eax,0
	call _printf 
	sub esp,4
	
	lea eax,[ebp-0xff8] ; n
	mov [esp+4],eax 
	mov dword [esp+0],LC2 
	mov eax,0 
	call _scanf
	sub esp,8
	
	mov dword [ebp-0xffc],0 ; i 
	jmp .Lmain_400
	
.Lmain_500:
	mov eax,[ebp-0xffc] ; i 
	lea edi,[ebp-0xfe0] ; a
	mov cl,[edi+eax] ; a[i]
	sub cl,48 ; '0'
	movsx ecx,cl
	cmp ecx,[ebp-0xff8] ;n
	jl .Lmain_490
	
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a 
	cmp byte [edi+eax],65 ; 'A'
	jge .Lmain_490 
	jmp .Lmain_489
	
.Lmain_490:
	mov eax,[ebp-0xffc] ; i 
	lea edi,[ebp-0xfe0] ;a 
	mov cl,[edi+eax] ; a[i] 
	sub cl,65 ; 'A'
	add cl,10
	movsx ecx,cl
	cmp ecx,[ebp-0xff8] ; n
	jl .Lmain_480
	
.Lmain_489:
	mov dword [esp+0],LC3 
	mov eax,0 
	call _printf 
	sub esp,4
	
	mov dword [esp+0],0
	call _exit 
	sub esp,4
	
.Lmain_480:
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a 
	cmp byte [edi+eax],48 ;'0'
	jl .Lmain_470
	
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a 
	cmp byte [edi+eax],57 ;'9'
	jg .Lmain_470
	
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a
	mov cl,[edi+eax] ; a[i]
	sub cl,48 ; '0'
	movsx ecx,cl 
	mov [ebp-0xff4],ecx ; t
	
	jmp .Lmain_460 
	
.Lmain_470:
	cmp dword [ebp-0xff8],11 ; n 
	jl .Lmain_460
	
	mov eax,[ebp-0xffc] ; i 
	lea edi,[ebp-0xfe0] ; a 
	cmp byte [edi+eax],65 ; 'A' 
	jl .Lmain_460 
	
	mov al,65 ; 'A'
	add al,[ebp-0xff8] ; n 
	sub al,10
	mov cl,al
	
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a 
	cmp cl,[edi+eax] 
	jl .Lmain_460 
	
	mov eax,[ebp-0xffc] ;i 
	lea edi,[ebp-0xfe0] ;a 
	mov al,[edi+eax]  ; a[i] 
	sub al,65 ;'A'
	add al,10
	mov [ebp-0xff4],al ; t
	
.Lmain_460:
	mov eax,[ebp-0x1000] ; t1 	
	imul eax, [ebp-0xff8] ; n 
	add eax,[ebp-0xff4] ; t 
	mov [ebp-0x1000],eax ; t1
	
	add dword [ebp-0xffc],1 ;i
.Lmain_400:
	mov eax,[ebp-0xffc] ; i 
	cmp [ebp-0xff0],eax ; t3
	jg .Lmain_500
	
	mov eax,[ebp-0x1000] ; t1 
	mov [esp+4],eax 
	mov dword [esp+0],LC4 
	mov eax,0 
	call _printf 
	sub esp,8
	
	mov eax,0
	leave
	ret